package org.as3search.criteria {
	/*
	 * as3search - http://code.google.com/p/as3search
	 * Copyright (c) 2009-2010 by respective authors. All rights reserved.
	 *
	 * This library is free software; you can redistribute it and/or modify it under the
	 * terms of the GNU Lesser General Public License as published by the Free Software
	 * Foundation; either version 2.1 of the License, or (at your option) any later
	 * version.
	 *
	 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
	 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
	 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
	 *
	 * You should have received a copy of the GNU Lesser General Public License along
	 * with this library; if not, write to the Free Software Foundation, Inc.,
	 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
	 */
	
	/**
	 * A <code>Disjunction</code> is an implementation of <code>Junction
	 * </code> which expresses a compound "OR" condition. If any of the
	 * <code>Criterion</code> instances added to this instance using 
	 * <code>add()</code> evaluate as true, this <code>Disjunction</code>
	 * evaluates as true. 
	 * 
	 * @example The following block of code offers an example of what
	 * <code>Disjunction</code> is capable of doing as compared to 
	 * native AS3 code:
	 * <listing version="3.0">
	 * //the following expression
	 * new Disjunction()
	 * 	.add(Restrictions.eq("id", 0))
	 * 	.add(Restrictions.eq("name", "bob"))
	 * //evaluates the same as this one does in AS3
	 * if (value.id == 0 || value.name == "bob") {
	 *    
	 * }
	 * </listing>
	 */
	public class Disjunction implements Junction {
		
		private var criteria:Array = [];
		/**
		 * Creates a new <code>Disjunction</code> instance.
		 */
		public function Disjunction() {
		
		}
		/**
		 * @inheritDoc
		 */
		public function add(value:Criterion):Junction {
			criteria.push(value);			
			return this;
		}
		/**
		 * @inheritDoc
		 */
		public function evaluate(value:Object):Boolean {
			if (criteria.length == 0)
				return true;
			
			for each (var criterion:Criterion in criteria) {
				if (criterion.evaluate(value))
					return true;
			}
			
			return false;
		}
	}
}